package com.yun.studyprovider.conf;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TopicRabbitConfig {
    //绑定键
    public final static String man = "topic.man";
    public final static String woman = "topic.woman";

    @Bean
    public Queue firstTopicQueue(){
        return new Queue(TopicRabbitConfig.man);
    }

    @Bean
    public Queue secondTopicQueue(){
        return new Queue(TopicRabbitConfig.woman);
    }

    @Bean
    public TopicExchange exchange(){
        return new TopicExchange("topicExchange");
    }


    /**
     * 将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
     * 这样只要是消息携带的路由键是topic.man,才会分发到该队列
     */
    @Bean
    public Binding bindingExchangeMessage(){
        return BindingBuilder.bind(firstTopicQueue()).to(exchange()).with(man);
    }

    /**
     * 将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
     * 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
     *
     * 其中“#”表示所有、全部的意思；“*”只匹配到一个词。
     *  ex:
     *      String[] routingKey = ['happy.work','happy.life','happy.work.teacher','sad.work','sad.life','sad.work.teacher']
     *
     *      "#"：匹配所有的路由键
     *
     *      "happy.#"：匹配  'happy.work',  'happy.life' , 'happy.work.teacher'
     *
     *      "work.#"：无匹配
     *
     *      “happy.*”：匹配 'happy.work',  'happy.life'
     *
     *      "*.work"：匹配 'happy.work',  'sad.work'
     *
     *      "*.work.#"：匹配  'happy.work',  'happy.work.teacher',  'sad.work', 'sad.work.teacher'
     */
    @Bean
    public Binding bindingExchangeMessage2(){
        return BindingBuilder.bind(secondTopicQueue()).to(exchange()).with("topic.#");
    }
}
